using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using AAM.BIZ;
using CMS.BOP;

namespace CMS.BIZ
{
    [Serializable]
    public class BizMembershipUser : MembershipUser, IBizObject
    {

        public static readonly ObjectType ObjectType = new ObjectType("BizMembershipUser");

        private ObjectID id;

        private string lastName = "";
        private string firstName = "";
        private string imagePath = "";
        private string cultureCode = "";
        private int? godfatherID = null;

        private BizMembershipUser godfather = null;

        public BizMembershipUser()
            : base()
        {
            id = null;
        }

        public BizMembershipUser(string providerName, string name, Object providerUserKey,
            string email, string passwordQuestion, string comment, bool isApproved,
            bool isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
            DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
            : base(providerName,name,providerUserKey,email,passwordQuestion,comment,isApproved,
                isLockedOut,creationDate,lastLoginDate,lastActivityDate,lastPasswordChangedDate,
                lastLockoutDate)
        {
            id = new ObjectID(ObjectType, new object[] { providerUserKey });
        }

        public int MemberID { get { return (int)ProviderUserKey; } }
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }
        public string ImagePath
        {
            get { return imagePath; }
            set { imagePath = value; }
        }
        public string CultureCode
        {
            get { return cultureCode; }
            set { cultureCode = value; }
        }
        public int? GodfatherID
        {
            get { return godfatherID; }
            set { godfatherID = value; }
        }
        public BizMembershipUser Godfather
        {
            get
            {
                if (godfather == null && godfatherID != null)
                {
                    godfather = (BizMembershipUser)Membership.GetUser((int)godfatherID, false);
                }
                return godfather;
            }
            set
            {
                if (value == null) GodfatherID = null;
                else GodfatherID = value.MemberID;
                godfather = value;
            }
        }

        public List<BizMembershipUser> Godchildren
        {
            get { return ((SqlBizMembershipProvider)Membership.Provider).ReadAllByGodfatherID(MemberID); }
        }

        public bool IsValid
        {
            get { return IsApproved && !IsLockedOut; }
        }

        public override bool Equals(object obj)
        {
            if (!(obj is IBizObject)) return false;
            IBizObject other = (IBizObject)obj;
            return id.Equals(other.ID);
        }

        public override int GetHashCode()
        {
            return id.GetHashCode();
        }

        public override string ToString()
        {
            return id.ToString();
        }

        #region IBizObject Members

        public ObjectID ID { get { if (id == null || id[0] == null) id = new ObjectID(ObjectType, new object[] { ProviderUserKey }); return id; } }

        public ObjectType Type { get { return ID.Type; } }

        public string TypeName { get { return Type.Name; } }

        public int UserID { get { return (int)ProviderUserKey; } }

        #endregion

        public bool LockUser()
        {
            SqlBizMembershipProvider provider = (SqlBizMembershipProvider)Membership.Provider;
            return provider.LockUser(UserName);
        }
    }
}
